﻿var waybill_storelook = {
    init: function() {
        var that = this;
        $.ajax({
            url: COMMONURL + "/api/bidload/GetLoadView?token=" + $.cookie('token'),
            type: "post",
            data: {
                page: 1,
                pagesize: 20
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000') {
                var compiled = _.template($("#waybillStoreLookTpl").html());
                var odata = redata.mod.Items;
                //alert(odata[0].load_time)
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].load_time=(odata[i].load_time!=null && odata[i].load_time!='')?odata[i].load_time.replace('T',' ').replace('Z',''):'';
                }
                 //alert(odata[0].load_time)

                $("#waybillStoreLookCon").html(compiled(odata));

           
                $("#storelookldemo").show().jqPaginator({
                   //totalPages: Math.ceil(redata.mod.TotalNum/20),
                   totalPages: redata.mod.TotalPageCount,
                   visiblePages: 5,
                   currentPage: 1,
                   first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                   prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                   next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                   last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                   page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                   onPageChange: function (num, type) {

                       $("#wstorepage").val(num);
                       if (type == 'change') {
                           that.doPage();
                       }
                   }
               });


                //关闭
                $('#closecpickModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        bidid = button.data('itemid'),
                        bid_no = button.data('bno'),
                        load_no = button.data('loadno');
                    $(this).attr({'bidid':bidid,'bid_no':bid_no,'load_no':load_no});


                });
                     //关闭提交
                $("#closereceiptSure").click(function() {
                    var bidid = $('#closecpickModel').attr('bidid'),
                        bid_no = $('#closecpickModel').attr('bid_no'),
                        load_no = $('#closecpickModel').attr('load_no'),

                        transport_status = $("#closedelTransport option:selected").val(),
                        goods_status = $("#closedelType option:selected").val(),
                         damage_flag = $('input[name="closetoarea"]:checked').val(),
                         damage = ($("#closeexampleInputAmount").val()==null)?0:$("#closeexampleInputAmount").val(),
                         freeze_funds = ($("#ecloseexampleInputAmount").val()==null)?0:$("#ecloseexampleInputAmount").val(),
                         reason_info = $.trim($("#closemessageText").val()),
                         contacts = $("#closecontacts").val(),
                         contacts_phone = $("#closecontacts_phone").val();
                         //alert(load_no);
                     $.ajax({
                          url: COMMONURL + "api/bidload/ClsoeBid?token=" + $.cookie('token'),
                          type: "post",
                          data: {
                              bid_id: bidid,
                              bid_no:bid_no,
                              load_no:load_no,
                              reason: transport_status,
                              goods_info: goods_status,
                              damage_flag: damage_flag,
                              damage: damage,
                              freeze_funds:freeze_funds,
                              reason_info:reason_info,
                              contacts: contacts,
                              contacts_phone: contacts_phone
                          }
                      }).done(function(redata) {
                          if(redata.recode == '0000'){
                            alert('操作成功');
                            $('#receiptModel').modal('hide');
                            window.location.reload();
                          }
                      });

                });

                //直接废标
                $('#lfpickModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        bid = button.data('bid'),
                        load_no = button.data('loadno');
                    $(this).attr({
                        'bid': bid,
                        'load_no':load_no
                    });

                     $("#ffremarkSubmit").on("click", function() {

                    var bid = $('#lfpickModel').attr('bid'),
                    load_no = $('#lfpickModel').attr('load_no'),

                        damage_flag = $('input[name="store-type-ffmodal"]:checked').val(),
                        damage = ($("#fstore-price").val()==null)?0:$("#fstore-price").val(),
                        close_reason = $("#lfReason").text(),
                        reason_info = $.trim($("#freasonInfo").val());
                         //alert(load_no);
                    $.ajax({
                        url: COMMONURL + "api/BidLoad/ClsoeBid?token=" + $.cookie('token'),
                        type: "post",
                        data: {
                            bid_id: bid,
                            load_no:load_no,
                            damage_flag: damage_flag,
                            damage: damage,
                            close_reason: close_reason,
                            reason_info: reason_info
                        }
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            alert('废标成功');
                            window.location.reload();
                        }
                    });


                })
                    
                });

                //重新选标
                $('#lselModel').on('show.bs.modal', function(event) {
                   var button = $(event.relatedTarget),
                        bid = button.data('bid'),
                        load_no = button.data('loadno');
                     $(this).attr({
                        'bid': bid,
                        'load_no':load_no
                    });
                    $.ajax({
                        url: COMMONURL + "api/BidPrice/GetRePriceList?token="+$.cookie('token'),
                        type: "get",
                        data: {
                            bid_id: bid
                        },
                        dataType: "json"
                    }).done(function(redata) {
                        if(redata.recode == '0000'){
                            var data = redata.mod;
                            var compiled = _.template($("#lselTpl").html());
                            $("#lselModelCon").html(compiled(data));
                        }
                    });
                });

                //选定
                $('#lselModelCon').on('click','[data-name="lselBid"]',function(){
                    if($(this).hasClass("disable")) return;
                    var bid_wl_id = $(this).attr('data-itemid');
                    var bid_id = $(this).attr('data-mybid');
                    $.ajax({
                        url:COMMONURL+"api/BidLoad/ReNotPickUp?token="+$.cookie('token'),
                        type:"post",
                        data:{
                            "wuliu_id":bid_wl_id,
                            //"price_id":bid_wl_id,
                            "bid_id":bid_id
                        },
                        dataType:"json"
                    }).done(function(redata){
                        if(redata.recode =='0000'){
                            alert(redata.resmessage);
                            window.location.reload(); 
                        }
                    });
                });


                //备注
                $('#lremarkModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        id = button.data('id');

                    $(this).attr({
                        'aid': id
                    });
                });
                //备注提交
                $("#lremarkSubmit").click(function() {
                    var id = $('#lremarkModel').attr('aid'),
                        remark = $.trim($("#lmessage-text").val());
                    $.ajax({
                        url: COMMONURL + "api/BidLoad/UpdateRemark?token=" + $.cookie('token'),
                        type: "post",
                        data: {
                            id: id,
                            load_remark: remark
                        }
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            alert('备注修改成功');
                            window.location.reload();
                        }
                    });

                });


                //未取货
                $('#lnopickModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        bid = button.data('bid'),
                        load_no = button.data('loadno');
                    $(this).attr({
                        'bid': bid,
                        'load_no':load_no
                    });
                    $.ajax({
                        url: COMMONURL + "api/BidPrice/GetRePriceList?token=" + $.cookie('token'),
                        type: "get",
                        data: {
                            bid_id: bid
                        },
                        dataType: "json"
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            var data = redata.mod;
                            for (var i = 0, length = data.length; i < length; i++) {
                                data[i].operator_time = that.format(data[i].operator_time);
                            }
                            var compiled = _.template($("#lnopicktable").html());
                            $(".ljtbody").html(compiled(data));

                        }
                    });
                });

                $('.ljtbody').on("click", '[data-name="lnopickBid"]', function() {

                        if ($(this).hasClass("chs")) return;
                        var index = $('.ljtbody').find('[data-name="lnopickBid"]').index($(this));
                  
                        $('.ljtbody').find('[data-name="lnopickBid"]').removeClass("chs").eq(index).addClass("chs");
                        var pid = $(this).attr("data-itemid");
                        $(this).parents(".ljtbody").attr("price_id", pid)

                    })
                    //未取货 重选 提交
                $("#lremarkfSubmit").on("click", function() {

                    var bid = $('#lnopickModel').attr('bid'),
                    load_no = $('#lnopickModel').attr('load_no'),
                        damage_flag = $('input[name="store-type-lmodal"]:checked').val(),
                        damage = ($("#lstore-price").val()==null)?0:$("#lstore-price").val(),
                        reason = $.trim($("#lreaseonStore").val()),
                    price_id = $(".ljtbody").attr("price_id");

                    $.ajax({
                        url: COMMONURL + "api/BidLoad/NotPickUp?token=" + $.cookie('token') + '&price_id=' + price_id,
                        type: "post",
                        data: {
                            bid_id: bid,
                            load_no:load_no,
                            damage_flag: damage_flag,
                            damage: damage,
                            reason: reason

                        }
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            alert('重选成功');
                            window.location.reload();
                        }
                    });


                })

                //未取货 废标 提交
                $("#lfremarkSubmit").on("click", function() {

                    var bid = $('#lnopickModel').attr('bid'),
                    load_no = $('#lnopickModel').attr('load_no'),
                        damage_flag = $('input[name="store-type-lmodal1"]:checked').val(),
                        damage = ($("#lstore-price1").val()==null)?0:$("#lstore-price1").val(),
                        close_reason = $("#lcloseReason").text(),
                        reason_info = $.trim($("#lreasonInfo").val());
                    $.ajax({
                        url: COMMONURL + "api/BidLoad/ClsoeBid?token=" + $.cookie('token'),
                        type: "post",
                        data: {
                            bid_id: bid,
                            load_no:load_no,
                            damage_flag: damage_flag,
                            damage: damage,
                            close_reason: close_reason,
                            reason_info: reason_info
                        }
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            alert('废标成功');
                            window.location.reload();
                        }
                    });


                })



                $(".ljnoPickBtn").on("click", function() {
                    var index = $(".ljnoPickBtn").index($(this));
                    $(".ljnoPickBtn").removeClass("current").eq(index).addClass("current");
                    $(".ljmodalStorecont").hide().eq(index).show();
                })


            }
        });
    },
    doPage:function(){
        var that = this;
        var page = $.trim($("#wstorepage").val());
        //alert(page);
        $.ajax({
            url: COMMONURL + "/api/bidload/GetLoadView?token=" + $.cookie('token'),
            type:"post",
            data:{
                "page":page,
                "pagesize":20
            },
            dataType:"json"
        }).done(function(redata){
            if (redata.recode == '0000' && redata.mod.Items.length>0) {
                var compiled = _.template($("#waybillStoreLookTpl").html());
                var odata = redata.mod.Items;
                //alert(odata[0].load_time)
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].load_time = (odata[i].load_time != null && odata[i].load_time != '') ? odata[i].load_time.replace('T', ' ').replace('Z', '') : '';
                }

                $("#waybillStoreLookCon").html(compiled(odata));
   
            }else{
                var compiled = _.template($("#waybillStoreLookTpl").html());
                var odata = redata.mod.Items;
                $("#waybillStoreLookCon").html(compiled(redata.mod));
                $("#storelookldemo").hide();
            }
        });
    },
    format: function(nS) {
         //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
             // return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17);
                if(!nS) return null;
            var date = new Date(nS*1000);
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            var d = date.getDate();
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            return year+'-'+month+'-'+d+' '+hours+':'+minutes;
    }
}
waybill_storelook.init();
